package com.xfreelance.qcshop.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xfreelance.qcshop.commom.RestBody;
import cn.hutool.http.HttpRequest;
import com.xfreelance.qcshop.entity.Goods;
import com.xfreelance.qcshop.exception.DptException;
import com.xfreelance.qcshop.service.GoodsService;
import com.xfreelance.qcshop.utils.DptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private DptUtil dptUtil;


    @GetMapping("/reword")
    public RestBody rewardGoods() {

        List<String> txmList= new ArrayList<String>() {
            {
                add("6910019016081");
                add("6910019023324");
                add("6910019022563");
                add("6910019015503");
                add("6970032250138");
                add("6910019023416");
            }
        };

        List<Map<String, Object>> goodsList = new ArrayList<>();

        for (String txm : txmList) {
            try {
                Map<String, Object> goodsInfo = goodsService.showTagInfo(txm);
                goodsList.add(goodsInfo);
            } catch (Exception e) {
                return RestBody.fail(e.getMessage());
            }
        }
        return RestBody.success(goodsList);
    }

    @GetMapping("/taginfo/{txm}")
    public RestBody taginfo(@PathVariable String txm) {
        try {
            List<Map<String, Object>> goodsList = new ArrayList<>();
            Map<String, Object> goodsInfo = goodsService.showTagInfo(txm);
            goodsList.add(goodsInfo);
            return RestBody.success(goodsList);
        } catch (Exception e) {
            return RestBody.fail(e.getMessage());
        }
    }

    @GetMapping("/bonus")
    public RestBody bonus() {
        try {
            List<Map<String, Object>> goodsList = goodsService.getBonusGoods(null);
            return RestBody.success(goodsList);
        } catch (Exception e) {
            return RestBody.fail(e.getMessage());
        }
    }

    @GetMapping("/detail/{code}")
    public RestBody detail(@PathVariable String code) {
        Map<String, Object> restInfo = new HashMap<>();
        try {
            String accCode = dptUtil.getAccCode(code);
            Map<String, Object> detailInfo = dptUtil.getDetailInfo(accCode);
            // 存储到本地表
            Goods goods = goodsService.getGoodsByCode(code);
            if (goods == null) {
                goods = new Goods();
                goods.setCode(code);
                goods.setName(detailInfo.get("商品名称").toString());
                goodsService.add(goods);
            }
            // 获取促销信息
            Map<String, Object> promotionInfo = dptUtil.getPromotion(accCode);
            // 构造返回值
            restInfo.put("detail", detailInfo);
            restInfo.put("promotion", promotionInfo);
            restInfo.put("images", new String[]{"/images/zjnxjj.png",
                    "https://6qsabok0kef0.ngrok.xiaomiqiu123.top/image/微信图片_20240325132448.jpg",
                    "https://6qsabok0kef0.ngrok.xiaomiqiu123.top/image/微信图片_20240325132446.jpg"});

            return RestBody.success(restInfo);
        } catch (DptException e) {
            // 商品不存在
            return RestBody.fail(e.getMessage());
        }
    }
}
